//
//  KFSearchViewController.m
//  SearchHistory
//
//  Created by VS on 16/5/9.
//  Copyright © 2016年 VS. All rights reserved.
//

#define kSearchHistoryKey @"SearchHistoryKey"

#import "KFSearchViewController.h"
#import "KFSearchResultViewController.h"

#import "KFHotSearchCell.h"
#import "KFSearchHistoryView.h"
#import "KFSearchHistoryCell.h"

@interface KFSearchViewController () <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, KFSearchHistoryCellDeleagte>

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) BOOL isHistory;
@property (strong, nonatomic) NSMutableArray *historyArray;

@end

@implementation KFSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.titleView = _searchBar;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *historyList = [defaults valueForKey:kSearchHistoryKey];
    if ((historyList.count != 0) && (![historyList isKindOfClass:[NSNull class]])) {
        self.historyArray = [historyList mutableCopy];
    }
}

- (NSMutableArray *)historyArray {
    if (!_historyArray) {
        _historyArray = [NSMutableArray array];
    }
    return _historyArray;
}

- (void)serachResultWithKeyWord:(NSString *)text {
    if ([text isEqualToString:@""]) {
        return;
    }
    
    if ([self.historyArray count] > 0) {
        if (![self.historyArray containsObject:text]) {
            [self.historyArray insertObject:text atIndex:0];
        }
    }else{
        [self.historyArray insertObject:text atIndex:0];
    }
    
    
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.historyArray forKey:kSearchHistoryKey];
    [defaults synchronize];
    [_tableView reloadData];
    
    KFSearchResultViewController *vc = [[KFSearchResultViewController alloc] init];
    vc.searchKeyWord = text;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewdelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.historyArray.count == 0 ? 1 : 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.historyArray.count == 0) {
        return 1;
    } else {
        if (section == 0) {
            return self.historyArray.count;
        } else if (section == 1) {
            return 1;
        }
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.historyArray.count == 0) {
        KFHotSearchCell *cell = [KFHotSearchCell hotSearchCellWithTableView:tableView];
        return cell;
    } else {
        if (indexPath.section == 0) {
            KFSearchHistoryCell *cell = [KFSearchHistoryCell searchHistoryCellWithTableView:tableView indexPath:indexPath];
            if (self.historyArray.count != 0) {
                 cell.text = self.historyArray[indexPath.row];
            }
            cell.delegate = self;
            return cell;
        } else if (indexPath.section == 1){
            KFHotSearchCell *cell = [KFHotSearchCell hotSearchCellWithTableView:tableView];
            return cell;
        }
    }
    
    return [[UITableViewCell alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.historyArray.count == 0) {
        return 300*FTY_HEIGHT_BASE;
    } else {
        if (indexPath.section == 0) {
            return 45*FTY_HEIGHT_BASE;
        }else{
            return 300*FTY_HEIGHT_BASE;
        }
    }
}


#pragma mark - UISerachBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar endEditing:YES];
    [self serachResultWithKeyWord:searchBar.text];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"%@",searchText);
}

#pragma mark -  KFSearchHistoryCellDeleagte
- (void)onDelSearchHistoryRecord:(KFSearchHistoryCell *)cell {
    
    NSIndexPath *indexPath = cell.indexPath;
    [self.historyArray removeObjectAtIndex:indexPath.row];
    if (self.historyArray.count != 0) {
     [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.historyArray forKey:kSearchHistoryKey];
    [defaults synchronize];
    [_tableView reloadData];
}

- (void)searchHistoryCellOnTap:(KFSearchHistoryCell *)cell {
    KFSearchResultViewController *vc = [[KFSearchResultViewController alloc] init];
    vc.title = self.historyArray[cell.indexPath.row];
    vc.searchKeyWord = self.historyArray[cell.indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
